import scrapy

from ..items import DangdangspiderItem


class DangdangSpider(scrapy.Spider):
    name = 'dangdang'

    def start_requests(self):

        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"
        }
        url_format = 'http://category.dangdang.com/pg{}-cp01.01.00.00.00.00.html'
        for i in range(1,61):
            url = url_format.format(i)
            yield scrapy.Request(url=url, headers=headers)
        # yield scrapy.Request(url=url_format, headers=headers)

    def parse(self, response):
        content = response.text
        li_list = response.xpath("//div[@id='search_nature_rg']/ul/li")
        for li in li_list:
            title = li.xpath("./p[@class='name']/a/text()")[0].extract()
            price = li.xpath("./p[@class='price']/span[1]/text()")[0].extract()
            try:
                author = li.xpath("./p[@class='search_book_author']/span[1]/a/text()")[0].extract()
            except IndexError as e:
                try:
                    author = li.xpath("./p[@class='search_book_author']/span[1]/text()")[0].extract()
                except IndexError as e:
                    author = ''
            try:
                time = li.xpath("./p[@class='search_book_author']/span[2]/text()")[0].extract().split('/')[1]
            except IndexError as e:
                time = ''
            try:
                P_cbs = li.xpath("./p[@class='search_book_author']/span[3]/a/text()")[0].extract()
            except IndexError as e:
                P_cbs = ''
            try:
                detail = li.xpath("./p[@class='detail']/text()")[0].extract()
            except IndexError as e:
                detail = ''

            book = DangdangspiderItem()
            book['title'] = title
            book['price'] = price
            book['author'] = author
            book['time'] = time
            book['P_cbs'] = P_cbs
            book['detail'] = detail

            yield book
